<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Awk 备忘清单
 &#x26;  awk cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="这是 GNU awk 的单页快速参考备忘单，其中涵盖了常用的 awk 表达式和命令。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="awk,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/awk.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.2"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="awk-备忘清单"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" fill="currentColor" height="1em" width="1em">
  <path d="M107.946667 838.4l57.173333 23.893333v-385.28l-103.68 250.026667c-17.493333 43.52 3.413333 93.44 46.506667 111.36z m832-157.866667L728.32 169.813333a85.888 85.888 0 0 0-77.226667-52.48c-11.093333 0-22.613333 1.706667-33.706666 6.4L302.933333 253.866667a85.290667 85.290667 0 0 0-46.08 110.933333l211.626667 510.72a85.248 85.248 0 0 0 110.933333 46.08l314.026667-130.133333a85.077333 85.077333 0 0 0 46.506667-110.933334zM336.213333 373.333333c-23.466667 0-42.666667-19.2-42.666666-42.666666s19.2-42.666667 42.666666-42.666667 42.666667 19.2 42.666667 42.666667-19.2 42.666667-42.666667 42.666666z m-85.333333 469.333334c0 46.933333 38.4 85.333333 85.333333 85.333333h61.866667l-147.2-355.84v270.506667z"></path>
</svg><a aria-hidden="true" tabindex="-1" href="#awk-备忘清单"><span class="icon icon-link"></span></a>Awk 备忘清单</h1><div class="wrap-body">
<p>这是 <a href="https://www.gnu.org/software/gawk/manual/gawk.html">GNU awk</a> 的单页快速参考备忘单，其中涵盖了常用的 <code>awk</code> 表达式和命令。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#试试">试试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#awk-程序">Awk 程序</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#示例">示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#awk-程序示例">Awk 程序示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#条件">条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#生成-1000-个空格">生成 1000 个空格</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组-arrays">数组 Arrays</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数-functions">函数 Functions</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-变量">Awk 变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#内置变量">内置变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#表达式">表达式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例-1">示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#仅限-gnu-awk">仅限 GNU awk</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#环境变量">环境变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义变量">定义变量</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#使用-shell-变量">使用 shell 变量</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-运算符">Awk 运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符">运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#正则表达式">正则表达式</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#更多条件">更多条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符-1">运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#算术运算">算术运算</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#速记作业">速记作业</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#比较运算符">比较运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例-2">示例</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#不匹配">不匹配</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#如果在数组中">如果在数组中</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-函数">Awk 函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用功能">常用功能</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用户定义函数">用户定义函数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-数组">Awk 数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#带索引的数组">带索引的数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#带键的数组">带键的数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#带拆分的数组">带拆分的数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#带有排序的数组">带有排序的数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多维">多维</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多维迭代">多维迭代</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-条件">Awk 条件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-else-语句">if-else 语句</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#三元运算符">三元运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#存在">存在</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#不存在">不存在</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch">switch</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-循环">Awk 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#fori">for...i</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#1-到-100-之间的-2-的幂">1 到 100 之间的 2 的幂</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#forin">for...in</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#arguments">Arguments</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例-3">示例</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#反向记录">反向记录</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#反向字段">反向字段</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#按记录求和">按记录求和</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#总结整个文件">总结整个文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while">while</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#dowhile">do...while</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#break">Break</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#continue">Continue</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#awk-格式化打印">Awk 格式化打印</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用法">用法</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#右对齐">右对齐</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#左对齐">左对齐</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#通用说明符">通用说明符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#space-空间">Space 空间</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#header-标题头">Header 标题头</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#各种各样的">各种各样的</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#正则表达式元字符">正则表达式元字符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#转义序列">转义序列</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运行脚本">运行脚本</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="试试"><a aria-hidden="true" tabindex="-1" href="#试试"><span class="icon icon-link"></span></a>试试</h3><div class="wrap-body">
<p>该程序可用于选择文件中的特定记录并对其执行操作</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">awk</span> -F: <span class="token string">'{print $1, $NF}'</span> /etc/passwd
</span></code></pre>
<hr>








































<table><thead><tr><th>-</th><th>-</th><th>-</th></tr></thead><tbody><tr><td></td><td><code>-F:</code></td><td>冒号作为分隔符</td></tr><tr><td></td><td><code>{...}</code></td><td>awk 程序</td></tr><tr><td></td><td><code>print</code></td><td>打印当前记录</td></tr><tr><td></td><td><code>$1</code></td><td>第一个字段</td></tr><tr><td></td><td><code>$NF</code></td><td>最后一个字段</td></tr><tr><td></td><td><code>/etc/passwd</code></td><td>输入数据文件</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="awk-程序"><a aria-hidden="true" tabindex="-1" href="#awk-程序"><span class="icon icon-link"></span></a>Awk 程序</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">BEGIN          <span class="token punctuation">{</span><span class="token operator">&#x3C;</span>初始化<span class="token operator">></span><span class="token punctuation">}</span> 
</span><span class="code-line">   <span class="token operator">&#x3C;</span>pattern <span class="token operator"><span class="token file-descriptor important">1</span>></span> <span class="token punctuation">{</span><span class="token operator">&#x3C;</span>计划动作<span class="token operator">></span><span class="token punctuation">}</span> 
</span><span class="code-line">   <span class="token operator">&#x3C;</span>pattern <span class="token operator"><span class="token file-descriptor important">2</span>></span> <span class="token punctuation">{</span><span class="token operator">&#x3C;</span>计划动作<span class="token operator">></span><span class="token punctuation">}</span> 
</span><span class="code-line">   <span class="token punctuation">..</span>.
</span><span class="code-line">END            <span class="token punctuation">{</span><span class="token operator">&#x3C;</span> 最后的动作 <span class="token operator">></span><span class="token punctuation">}</span>
</span></code></pre>
<h4 id="示例"><a aria-hidden="true" tabindex="-1" href="#示例"><span class="icon icon-link"></span></a>示例</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'
</span></span><span class="code-line"><span class="token string">    BEGIN { print "\n>>>Start" }
</span></span><span class="code-line"><span class="token string">    !/(login|shutdown)/ { print NR, $0 }
</span></span><span class="code-line"><span class="token string">    END { print "&#x3C;&#x3C;&#x3C;END\n" }
</span></span><span class="code-line"><span class="token string">'</span> /etc/passwd
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">          <span class="token variable">$1</span>      <span class="token variable">$2</span>/<span class="token variable"><span class="token variable">$(</span>NF-1<span class="token variable">)</span></span>    <span class="token variable">$3</span>/<span class="token variable">$NF</span>
</span><span class="code-line">           ▼          ▼           ▼ 
</span><span class="code-line">        ┌──────┬──────────────┬───────┐
</span><span class="code-line"><span class="token variable">$0</span>/NR ▶ │  ID  │  WEBSITE     │  URI  │
</span><span class="code-line">        ├──────┼──────────────┼───────┤
</span><span class="code-line"><span class="token variable">$0</span>/NR ▶ │  <span class="token number">1</span>   │  baidu.com   │  <span class="token function">awk</span>  │
</span><span class="code-line">        ├──────┼──────────────┼───────┤
</span><span class="code-line"><span class="token variable">$0</span>/NR ▶ │  <span class="token number">2</span>   │  google.com  │  <span class="token number">25</span>   │
</span><span class="code-line">        └──────┴──────────────┴───────┘
</span></code></pre>
<hr>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 第一个和最后一个字段</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{print $1,$NF}'</span> /etc/passwd
</span><span class="code-line"><span class="token comment"># 带行号</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{print NR, $0}'</span> /etc/passwd
</span><span class="code-line"><span class="token comment"># 倒数第二个字段</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{print $(NF-1)}'</span> /etc/passwd
</span><span class="code-line"><span class="token comment"># 自定义字符串</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{print $1 "=" $6}'</span> /etc/passwd
</span></code></pre>
<p>查看: <a href="#awk-%E5%8F%98%E9%87%8F">Awk 变量</a></p>
</div></div></div><div class="wrap h3body-not-exist row-span-2 col-span-2"><div class="wrap-header h3wrap"><h3 id="awk-程序示例"><a aria-hidden="true" tabindex="-1" href="#awk-程序示例"><span class="icon icon-link"></span></a>Awk 程序示例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2 col-span-2-->
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {print "hello world"}'</span>        <span class="token comment"># 打印 "hello world"</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{print $1}'</span> /etc/passwd         <span class="token comment"># -F: 指定字段分隔符</span>
</span><span class="code-line"><span class="token comment"># /pattern/ 仅对匹配的模式执行操作</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'/root/ {print $1}'</span> /etc/passwd                     
</span><span class="code-line"><span class="token comment"># BEGIN 块在开始时执行一次</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'BEGIN { print "uid"} { print $1 }'</span> /etc/passwd     
</span><span class="code-line"><span class="token comment"># END 块在最后执行一次</span>
</span><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{print $1} END { print "-done-"}'</span> /etc/passwd
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="条件"><a aria-hidden="true" tabindex="-1" href="#条件"><span class="icon icon-link"></span></a>条件</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'$3>30 {print $1}'</span> /etc/passwd
</span></code></pre>
<p>查看: <a href="#awk-%E6%9D%A1%E4%BB%B6">Conditions 条件</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="生成-1000-个空格"><a aria-hidden="true" tabindex="-1" href="#生成-1000-个空格"><span class="icon icon-link"></span></a>生成 1000 个空格</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN{
</span></span><span class="code-line"><span class="token string">    while (a++ &#x3C; 1000)
</span></span><span class="code-line"><span class="token string">        s=s " ";
</span></span><span class="code-line"><span class="token string">    print s
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<p>查看: <a href="#awk-%E5%BE%AA%E7%8E%AF">Loops</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组-arrays"><a aria-hidden="true" tabindex="-1" href="#数组-arrays"><span class="icon icon-link"></span></a>数组 Arrays</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">   fruits["mango"] = "yellow";
</span></span><span class="code-line"><span class="token string">   fruits["orange"] = "orange"
</span></span><span class="code-line"><span class="token string">   for(fruit in fruits) {
</span></span><span class="code-line"><span class="token string">     print fruit " 的颜色是 " fruits[fruit]
</span></span><span class="code-line"><span class="token string">   }
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<p>查看: <a href="#awk-%E6%95%B0%E7%BB%84">Awk 数组</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="函数-functions"><a aria-hidden="true" tabindex="-1" href="#函数-functions"><span class="icon icon-link"></span></a>函数 Functions</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># => 5</span>
</span><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN{print length("hello")}'</span>
</span><span class="code-line"><span class="token comment"># => HELLO</span>
</span><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN{print toupper("hello")}'</span>
</span><span class="code-line"><span class="token comment"># => hel</span>
</span><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN{print substr("hello", 1, 3)}'</span>
</span></code></pre>
<p>查看: <a href="#awk-%E5%87%BD%E6%95%B0">Functions</a></p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-变量"><a aria-hidden="true" tabindex="-1" href="#awk-变量"><span class="icon icon-link"></span></a>Awk 变量</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="内置变量"><a aria-hidden="true" tabindex="-1" href="#内置变量"><span class="icon icon-link"></span></a>内置变量</h3><div class="wrap-body">













































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>$0</code></td><td align="left">全线</td></tr><tr><td align="left"><code>$1, $2...$NF</code></td><td align="left">第一个，第二个……最后一个字段</td></tr><tr><td align="left"><code>NR</code></td><td align="left">记录总数(<code>N</code>umber of <code>R</code>ecords)</td></tr><tr><td align="left"><code>NF</code></td><td align="left">N个字段(<code>N</code>number of <code>F</code>ields)</td></tr><tr><td align="left"><code>OFS</code></td><td align="left"><code>O</code>utput <code>F</code>ield <code>S</code>eparator<br> 输出字段分隔符 <em>(default " ")</em></td></tr><tr><td align="left"><code>FS</code></td><td align="left">input <code>F</code>ield <code>S</code>eparator <br> 输入字段分隔符 <em>(default " ")</em></td></tr><tr><td align="left"><code>ORS</code></td><td align="left"><code>O</code>utput <code>R</code>ecord <code>S</code>eparator <br> 输出记录分隔符 <em>(default "\n")</em></td></tr><tr><td align="left"><code>RS</code></td><td align="left">input <code>R</code>ecord <code>S</code>eparator <br> 输入记录分隔符 <em>(default "\n")</em></td></tr><tr><td align="left"><code>FILENAME</code></td><td align="left">文件名</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="表达式"><a aria-hidden="true" tabindex="-1" href="#表达式"><span class="icon icon-link"></span></a>表达式</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->





































































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>$1 == "root"</code></td><td align="left">第一个字段等于根</td></tr><tr><td align="left"><code>{print $(NF-1)}</code></td><td align="left">倒数第二个字段</td></tr><tr><td align="left"><code>NR!=1{print $0}</code></td><td align="left">从第 2 条记录开始</td></tr><tr><td align="left"><code>NR > 3</code></td><td align="left">从第 4 条记录开始</td></tr><tr><td align="left"><code>NR == 1</code></td><td align="left">第一次记录</td></tr><tr><td align="left"><code>END{print NR}</code></td><td align="left">总记录</td></tr><tr><td align="left"><code>BEGIN{print OFMT}</code></td><td align="left">输出格式</td></tr><tr><td align="left"><code>{print NR, $0}</code></td><td align="left">行号</td></tr><tr><td align="left"><code>{print NR "  " $0}</code></td><td align="left">行号(选项卡)</td></tr><tr><td align="left"><code>{$1 = NR; print}</code></td><td align="left">用行号替换第一个字段</td></tr><tr><td align="left"><code>$NF > 4</code></td><td align="left">最后一个字段 > 4</td></tr><tr><td align="left"><code>NR % 2 == 0</code></td><td align="left">甚至记录</td></tr><tr><td align="left"><code>NR==10, NR==20</code></td><td align="left">记录 10 到 20</td></tr><tr><td align="left"><code>BEGIN{print ARGC}</code></td><td align="left">总 <code>arguments</code></td></tr><tr><td align="left"><code>ORS=NR%5?",":"\n"</code></td><td align="left">连接记录</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="示例-1"><a aria-hidden="true" tabindex="-1" href="#示例-1"><span class="icon icon-link"></span></a>示例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>打印总和和平均值</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{sum += $3}
</span></span><span class="code-line"><span class="token string">     END { print sum, sum/NR }
</span></span><span class="code-line"><span class="token string">'</span> /etc/passwd
</span></code></pre>
<p>打印参数</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    for (i = 1; i &#x3C; ARGC; i++)
</span></span><span class="code-line"><span class="token string">        print ARGV[i] }'</span> a b c
</span></code></pre>
<p>输出字段分隔符为逗号</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN { FS=":";OFS=","}
</span></span><span class="code-line"><span class="token string">    {print $1,$2,$3,$4}'</span> /etc/passwd
</span></code></pre>
<p>匹配位置</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    if (match("One Two Three", "Tw"))
</span></span><span class="code-line"><span class="token string">        print RSTART }'</span>
</span></code></pre>
<p>匹配时长</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    if (match("One Two Three", "re"))
</span></span><span class="code-line"><span class="token string">        print RLENGTH }'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="仅限-gnu-awk"><a aria-hidden="true" tabindex="-1" href="#仅限-gnu-awk"><span class="icon icon-link"></span></a>仅限 GNU awk</h3><div class="wrap-body">





























<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>ENVIRON</code></td><td align="left">环境变量</td></tr><tr><td align="left"><code>IGNORECASE</code></td><td align="left">忽略大小写</td></tr><tr><td align="left"><code>CONVFMT</code></td><td align="left">转换格式</td></tr><tr><td align="left"><code>ERRNO</code></td><td align="left">系统错误</td></tr><tr><td align="left"><code>FIELDWIDTHS</code></td><td align="left">固定宽度字段</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="环境变量"><a aria-hidden="true" tabindex="-1" href="#环境变量"><span class="icon icon-link"></span></a>环境变量</h3><div class="wrap-body">









































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>ARGC</code></td><td align="left">数字或参数</td></tr><tr><td align="left"><code>ARGV</code></td><td align="left">参数数组</td></tr><tr><td align="left"><code>FNR</code></td><td align="left">文件记录数(<code>F</code>ile <code>N</code>umber of <code>R</code>ecords)</td></tr><tr><td align="left"><code>OFMT</code></td><td align="left">数字格式 <em>(default "%.6g")</em></td></tr><tr><td align="left"><code>RSTART</code></td><td align="left">字符串中的位置</td></tr><tr><td align="left"><code>RLENGTH</code></td><td align="left">比赛时长</td></tr><tr><td align="left"><code>SUBSEP</code></td><td align="left">多维数组分隔符 <em>(default "\034")</em></td></tr><tr><td align="left"><code>ARGIND</code></td><td align="left">参数索引</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="定义变量"><a aria-hidden="true" tabindex="-1" href="#定义变量"><span class="icon icon-link"></span></a>定义变量</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token parameter variable">-v</span> <span class="token assign-left variable">var1</span><span class="token operator">=</span><span class="token string">"Hello"</span> <span class="token parameter variable">-v</span> <span class="token assign-left variable">var2</span><span class="token operator">=</span><span class="token string">"Wold"</span> <span class="token string">'
</span></span><span class="code-line"><span class="token string">    END {print var1, var2}
</span></span><span class="code-line"><span class="token string">'</span> <span class="token operator">&#x3C;</span>/dev/null
</span></code></pre>
<h4 id="使用-shell-变量"><a aria-hidden="true" tabindex="-1" href="#使用-shell-变量"><span class="icon icon-link"></span></a>使用 shell 变量</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token parameter variable">-v</span> <span class="token assign-left variable">varName</span><span class="token operator">=</span><span class="token string">"<span class="token environment constant">$PWD</span>"</span> <span class="token string">'
</span></span><span class="code-line"><span class="token string">    END {print varName}'</span> <span class="token operator">&#x3C;</span>/dev/null
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-运算符"><a aria-hidden="true" tabindex="-1" href="#awk-运算符"><span class="icon icon-link"></span></a>Awk 运算符</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="运算符"><a aria-hidden="true" tabindex="-1" href="#运算符"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">

























<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>{print $1}</code></td><td align="left">第一个字段</td></tr><tr><td align="left"><code>$2 == "foo"</code></td><td align="left">等于</td></tr><tr><td align="left"><code>$2 != "foo"</code></td><td align="left">不等于</td></tr><tr><td align="left"><code>"foo" in array</code></td><td align="left">在数组中</td></tr></tbody></table>
<h4 id="正则表达式"><a aria-hidden="true" tabindex="-1" href="#正则表达式"><span class="icon icon-link"></span></a>正则表达式</h4>

























<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>/regex/</code></td><td align="left">行匹配</td></tr><tr><td align="left"><code>!/regex/</code></td><td align="left">行不匹配</td></tr><tr><td align="left"><code>$1 ~ /regex/</code></td><td align="left">字段匹配</td></tr><tr><td align="left"><code>$1 !~ /regex/</code></td><td align="left">字段不匹配</td></tr></tbody></table>
<h4 id="更多条件"><a aria-hidden="true" tabindex="-1" href="#更多条件"><span class="icon icon-link"></span></a>更多条件</h4>

















<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>($2 &#x3C;= 4 || $3 &#x3C; 20)</code></td><td align="left">或者</td></tr><tr><td align="left"><code>($1 == 4 &#x26;&#x26; $3 &#x3C; 20)</code></td><td align="left">和</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="运算符-1"><a aria-hidden="true" tabindex="-1" href="#运算符-1"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">
<h4 id="算术运算"><a aria-hidden="true" tabindex="-1" href="#算术运算"><span class="icon icon-link"></span></a>算术运算</h4>
<ul class="cols-3 style-none">
<li><code>+</code></li>
<li><code>-</code></li>
<li><code>*</code></li>
<li><code>/</code></li>
<li><code>%</code></li>
<li><code>++</code></li>
<li><code>--</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
<h4 id="速记作业"><a aria-hidden="true" tabindex="-1" href="#速记作业"><span class="icon icon-link"></span></a>速记作业</h4>
<ul class="cols-3 style-none">
<li><code>+=</code></li>
<li><code>-=</code></li>
<li><code>*=</code></li>
<li><code>/=</code></li>
<li><code>%=</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
<h4 id="比较运算符"><a aria-hidden="true" tabindex="-1" href="#比较运算符"><span class="icon icon-link"></span></a>比较运算符</h4>
<ul class="cols-3 style-none">
<li><code>==</code></li>
<li><code>!=</code></li>
<li><code>&#x3C;</code></li>
<li><code>></code></li>
<li><code>&#x3C;=</code></li>
<li><code>>=</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="示例-2"><a aria-hidden="true" tabindex="-1" href="#示例-2"><span class="icon icon-link"></span></a>示例</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    if ("foo" ~ "^fo+$")
</span></span><span class="code-line"><span class="token string">        print "Fooey!";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="不匹配"><a aria-hidden="true" tabindex="-1" href="#不匹配"><span class="icon icon-link"></span></a>不匹配</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    if ("boo" !~ "^fo+$")
</span></span><span class="code-line"><span class="token string">        print "Boo!";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="如果在数组中"><a aria-hidden="true" tabindex="-1" href="#如果在数组中"><span class="icon icon-link"></span></a>如果在数组中</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    assoc["foo"] = "bar";
</span></span><span class="code-line"><span class="token string">    assoc["bar"] = "baz";
</span></span><span class="code-line"><span class="token string">    if ("foo" in assoc)
</span></span><span class="code-line"><span class="token string">        print "Fooey!";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-函数"><a aria-hidden="true" tabindex="-1" href="#awk-函数"><span class="icon icon-link"></span></a>Awk 函数</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="常用功能"><a aria-hidden="true" tabindex="-1" href="#常用功能"><span class="icon icon-link"></span></a>常用功能</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->

































































<table><thead><tr><th align="left">函数</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>index(s,t)</code></td><td align="left">字符串 <code>s</code> 中出现字符串 <code>t</code> 的位置，如果未找到则为 <code>0</code></td></tr><tr><td align="left"><code>length(s)</code></td><td align="left">字符串 <code>s</code> 的长度(如果没有 <code>arg</code>，则为 <code>$0</code>)</td></tr><tr><td align="left"><code>rand</code></td><td align="left"><code>0</code> 到 <code>1</code> 之间的随机数</td></tr><tr><td align="left"><code>substr(s,index,len)</code></td><td align="left">返回从索引开始的 <code>s</code> 的 <code>len-char</code> 子字符串(从 <code>1</code> 开始计数)</td></tr><tr><td align="left"><code>srand</code></td><td align="left">为 <code>rand</code> 设置种子并返回之前的种子</td></tr><tr><td align="left"><code>int(x)</code></td><td align="left">将 <code>x</code> 截断为整数值</td></tr><tr><td align="left"><code>split(s,a,fs)</code></td><td align="left">将字符串 <code>s</code> 拆分为数组 <code>a</code> 由 <code>fs</code> 拆分，返回 <code>a</code> 的长度</td></tr><tr><td align="left"><code>match(s,r)</code></td><td align="left">字符串 <code>s</code> 中出现正则表达式 <code>r</code> 的位置，如果未找到，则为 <code>0</code></td></tr><tr><td align="left"><code>sub(r,t,s)</code></td><td align="left">将 <code>t</code> 替换为字符串 <code>s</code> 中第一次出现的正则表达式 <code>r</code>(如果未给出 <code>s</code>，则替换为 <code>$0</code>)</td></tr><tr><td align="left"><code>gsub(r,t,s)</code></td><td align="left">用 <code>t</code> 替换字符串 <code>s</code> 中所有出现的正则表达式 <code>r</code></td></tr><tr><td align="left"><code>system(cmd)</code></td><td align="left">执行cmd并返回退出状态</td></tr><tr><td align="left"><code>tolower(s)</code></td><td align="left">字符串 <code>s</code> 转小写</td></tr><tr><td align="left"><code>toupper(s)</code></td><td align="left">字符串 <code>s</code> 转大写</td></tr><tr><td align="left"><code>getline</code></td><td align="left">将 <code>$0</code> 设置为当前输入文件中的下一个输入记录</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="用户定义函数"><a aria-hidden="true" tabindex="-1" href="#用户定义函数"><span class="icon icon-link"></span></a>用户定义函数</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'
</span></span><span class="code-line"><span class="token string">    # 返回最小数量
</span></span><span class="code-line"><span class="token string">    function find_min(num1, num2){
</span></span><span class="code-line"><span class="token string">       if (num1 &#x3C; num2)
</span></span><span class="code-line"><span class="token string">       return num1
</span></span><span class="code-line"><span class="token string">       return num2
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">    # 返回最大数量
</span></span><span class="code-line"><span class="token string">    function find_max(num1, num2){
</span></span><span class="code-line"><span class="token string">       if (num1 > num2)
</span></span><span class="code-line"><span class="token string">       return num1
</span></span><span class="code-line"><span class="token string">       return num2
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">    # 主功能
</span></span><span class="code-line"><span class="token string">    function main(num1, num2){
</span></span><span class="code-line"><span class="token string">       result = find_min(num1, num2)
</span></span><span class="code-line"><span class="token string">       print "Minimum =", result
</span></span><span class="code-line"><span class="token string">      
</span></span><span class="code-line"><span class="token string">       result = find_max(num1, num2)
</span></span><span class="code-line"><span class="token string">       print "Maximum =", result
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">    # 脚本执行从这里开始
</span></span><span class="code-line"><span class="token string">    BEGIN {
</span></span><span class="code-line"><span class="token string">       main(10, 60)
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">'</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-数组"><a aria-hidden="true" tabindex="-1" href="#awk-数组"><span class="icon icon-link"></span></a>Awk 数组</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="带索引的数组"><a aria-hidden="true" tabindex="-1" href="#带索引的数组"><span class="icon icon-link"></span></a>带索引的数组</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    arr[0] = "foo";
</span></span><span class="code-line"><span class="token string">    arr[1] = "bar";
</span></span><span class="code-line"><span class="token string">    print(arr[0]); # => foo
</span></span><span class="code-line"><span class="token string">    delete arr[0];
</span></span><span class="code-line"><span class="token string">    print(arr[0]); # => ""
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="带键的数组"><a aria-hidden="true" tabindex="-1" href="#带键的数组"><span class="icon icon-link"></span></a>带键的数组</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    assoc["foo"] = "bar";
</span></span><span class="code-line"><span class="token string">    assoc["bar"] = "baz";
</span></span><span class="code-line"><span class="token string">    print("baz" in assoc); # => 0
</span></span><span class="code-line"><span class="token string">    print("foo" in assoc); # => 1
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="带拆分的数组"><a aria-hidden="true" tabindex="-1" href="#带拆分的数组"><span class="icon icon-link"></span></a>带拆分的数组</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    split("foo:bar:baz", arr, ":");
</span></span><span class="code-line"><span class="token string">    for (key in arr)
</span></span><span class="code-line"><span class="token string">        print arr[key];
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="带有排序的数组"><a aria-hidden="true" tabindex="-1" href="#带有排序的数组"><span class="icon icon-link"></span></a>带有排序的数组</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    arr[0] = 3
</span></span><span class="code-line"><span class="token string">    arr[1] = 2
</span></span><span class="code-line"><span class="token string">    arr[2] = 4
</span></span><span class="code-line"><span class="token string">    n = asort(arr)
</span></span><span class="code-line"><span class="token string">    for (i = 1; i &#x3C;= n ; i++)
</span></span><span class="code-line"><span class="token string">        print(arr[i])
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多维"><a aria-hidden="true" tabindex="-1" href="#多维"><span class="icon icon-link"></span></a>多维</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    multidim[0,0] = "foo";
</span></span><span class="code-line"><span class="token string">    multidim[0,1] = "bar";
</span></span><span class="code-line"><span class="token string">    multidim[1,0] = "baz";
</span></span><span class="code-line"><span class="token string">    multidim[1,1] = "boo";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多维迭代"><a aria-hidden="true" tabindex="-1" href="#多维迭代"><span class="icon icon-link"></span></a>多维迭代</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    array[1,2]=3;
</span></span><span class="code-line"><span class="token string">    array[2,3]=5;
</span></span><span class="code-line"><span class="token string">    for (comb in array) {
</span></span><span class="code-line"><span class="token string">        split(comb,sep,SUBSEP);
</span></span><span class="code-line"><span class="token string">        print sep[1], sep[2], 
</span></span><span class="code-line"><span class="token string">        array[sep[1],sep[2]]
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-条件"><a aria-hidden="true" tabindex="-1" href="#awk-条件"><span class="icon icon-link"></span></a>Awk 条件</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="if-else-语句"><a aria-hidden="true" tabindex="-1" href="#if-else-语句"><span class="icon icon-link"></span></a>if-else 语句</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token parameter variable">-v</span> <span class="token assign-left variable">count</span><span class="token operator">=</span><span class="token number">2</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    if (count == 1)
</span></span><span class="code-line"><span class="token string">        print "Yes";
</span></span><span class="code-line"><span class="token string">    else
</span></span><span class="code-line"><span class="token string">        print "Huh?";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="三元运算符"><a aria-hidden="true" tabindex="-1" href="#三元运算符"><span class="icon icon-link"></span></a>三元运算符</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token parameter variable">-v</span> <span class="token assign-left variable">count</span><span class="token operator">=</span><span class="token number">2</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    print (count==1) ? "Yes" : "Huh?";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="存在"><a aria-hidden="true" tabindex="-1" href="#存在"><span class="icon icon-link"></span></a>存在</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    assoc["foo"] = "bar";
</span></span><span class="code-line"><span class="token string">    assoc["bar"] = "baz";
</span></span><span class="code-line"><span class="token string">    if ("foo" in assoc)
</span></span><span class="code-line"><span class="token string">        print "Fooey!";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="不存在"><a aria-hidden="true" tabindex="-1" href="#不存在"><span class="icon icon-link"></span></a>不存在</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    assoc["foo"] = "bar";
</span></span><span class="code-line"><span class="token string">    assoc["bar"] = "baz";
</span></span><span class="code-line"><span class="token string">    if ("Huh" in assoc == 0 )
</span></span><span class="code-line"><span class="token string">        print "Huh!";
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="switch"><a aria-hidden="true" tabindex="-1" href="#switch"><span class="icon icon-link"></span></a>switch</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{
</span></span><span class="code-line"><span class="token string">    switch (NR * 2 + 1) {
</span></span><span class="code-line"><span class="token string">        case 3:
</span></span><span class="code-line"><span class="token string">        case "11":
</span></span><span class="code-line"><span class="token string">            print NR - 1
</span></span><span class="code-line"><span class="token string">            break
</span></span><span class="code-line"><span class="token string">        case /2[[:digit:]]+/:
</span></span><span class="code-line"><span class="token string">            print NR
</span></span><span class="code-line"><span class="token string">        default:
</span></span><span class="code-line"><span class="token string">            print NR + 1
</span></span><span class="code-line"><span class="token string">        case -1:
</span></span><span class="code-line"><span class="token string">            print NR * -1
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">}'</span> /etc/passwd
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-循环"><a aria-hidden="true" tabindex="-1" href="#awk-循环"><span class="icon icon-link"></span></a>Awk 循环</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="fori"><a aria-hidden="true" tabindex="-1" href="#fori"><span class="icon icon-link"></span></a>for...i</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    for (i = 0; i &#x3C; 10; i++)
</span></span><span class="code-line"><span class="token string">        print "i=" i;
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="1-到-100-之间的-2-的幂"><a aria-hidden="true" tabindex="-1" href="#1-到-100-之间的-2-的幂"><span class="icon icon-link"></span></a>1 到 100 之间的 2 的幂</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    for (i = 1; i &#x3C;= 100; i *= 2)
</span></span><span class="code-line"><span class="token string">        print i
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="forin"><a aria-hidden="true" tabindex="-1" href="#forin"><span class="icon icon-link"></span></a>for...in</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    assoc["key1"] = "val1"
</span></span><span class="code-line"><span class="token string">    assoc["key2"] = "val2"
</span></span><span class="code-line"><span class="token string">    for (key in assoc)
</span></span><span class="code-line"><span class="token string">        print assoc[key];
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="arguments"><a aria-hidden="true" tabindex="-1" href="#arguments"><span class="icon icon-link"></span></a>Arguments</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    for (argnum in ARGV)
</span></span><span class="code-line"><span class="token string">        print ARGV[argnum];
</span></span><span class="code-line"><span class="token string">}'</span> a b c
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="示例-3"><a aria-hidden="true" tabindex="-1" href="#示例-3"><span class="icon icon-link"></span></a>示例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<h4 id="反向记录"><a aria-hidden="true" tabindex="-1" href="#反向记录"><span class="icon icon-link"></span></a>反向记录</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{ x[NR] = $0 }
</span></span><span class="code-line"><span class="token string">    END {
</span></span><span class="code-line"><span class="token string">        for (i = NR; i > 0; i--)
</span></span><span class="code-line"><span class="token string">        print x[i]
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">'</span> /etc/passwd
</span></code></pre>
<h4 id="反向字段"><a aria-hidden="true" tabindex="-1" href="#反向字段"><span class="icon icon-link"></span></a>反向字段</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{
</span></span><span class="code-line"><span class="token string">    for (i = NF; i > 0; i--)
</span></span><span class="code-line"><span class="token string">        printf("%s ",$i);
</span></span><span class="code-line"><span class="token string">    print ""
</span></span><span class="code-line"><span class="token string">}'</span> /etc/passwd
</span></code></pre>
<h4 id="按记录求和"><a aria-hidden="true" tabindex="-1" href="#按记录求和"><span class="icon icon-link"></span></a>按记录求和</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{
</span></span><span class="code-line"><span class="token string">    s=0;
</span></span><span class="code-line"><span class="token string">    for (i = 1; i &#x3C;= NF; i++)
</span></span><span class="code-line"><span class="token string">        s += $i;
</span></span><span class="code-line"><span class="token string">    print s
</span></span><span class="code-line"><span class="token string">}'</span> /etc/passwd
</span></code></pre>
<h4 id="总结整个文件"><a aria-hidden="true" tabindex="-1" href="#总结整个文件"><span class="icon icon-link"></span></a>总结整个文件</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'
</span></span><span class="code-line"><span class="token string">    {for (i = 1; i &#x3C;= NF; i++)
</span></span><span class="code-line"><span class="token string">        s += $i;
</span></span><span class="code-line"><span class="token string">    };
</span></span><span class="code-line"><span class="token string">    END{print s}
</span></span><span class="code-line"><span class="token string">'</span> /etc/passwd
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="while"><a aria-hidden="true" tabindex="-1" href="#while"><span class="icon icon-link"></span></a>while</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    while (a &#x3C; 10) {
</span></span><span class="code-line"><span class="token string">        print "- " " concatenation: " a
</span></span><span class="code-line"><span class="token string">        a++;
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
<h4 id="dowhile"><a aria-hidden="true" tabindex="-1" href="#dowhile"><span class="icon icon-link"></span></a>do...while</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'{
</span></span><span class="code-line"><span class="token string">    i = 1
</span></span><span class="code-line"><span class="token string">    do {
</span></span><span class="code-line"><span class="token string">        print $0
</span></span><span class="code-line"><span class="token string">        i++
</span></span><span class="code-line"><span class="token string">    } while (i &#x3C;= 5)
</span></span><span class="code-line"><span class="token string">}'</span> /etc/passwd
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="break"><a aria-hidden="true" tabindex="-1" href="#break"><span class="icon icon-link"></span></a>Break</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    break_num = 5
</span></span><span class="code-line"><span class="token string">    for (i = 0; i &#x3C; 10; i++) {
</span></span><span class="code-line"><span class="token string">        print i
</span></span><span class="code-line"><span class="token string">        if (i == break_num)
</span></span><span class="code-line"><span class="token string">            break
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="continue"><a aria-hidden="true" tabindex="-1" href="#continue"><span class="icon icon-link"></span></a>Continue</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    for (x = 0; x &#x3C;= 10; x++) {
</span></span><span class="code-line"><span class="token string">        if (x == 5 || x == 6)
</span></span><span class="code-line"><span class="token string">            continue
</span></span><span class="code-line"><span class="token string">        printf "%d ", x
</span></span><span class="code-line"><span class="token string">    }
</span></span><span class="code-line"><span class="token string">    print ""
</span></span><span class="code-line"><span class="token string">}'</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="awk-格式化打印"><a aria-hidden="true" tabindex="-1" href="#awk-格式化打印"><span class="icon icon-link"></span></a>Awk 格式化打印</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="用法"><a aria-hidden="true" tabindex="-1" href="#用法"><span class="icon icon-link"></span></a>用法</h3><div class="wrap-body">
<h4 id="右对齐"><a aria-hidden="true" tabindex="-1" href="#右对齐"><span class="icon icon-link"></span></a>右对齐</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN{printf "|%10s|\n", "hello"}'</span>
</span><span class="code-line"><span class="token comment"># |     hello|</span>
</span></code></pre>
<h4 id="左对齐"><a aria-hidden="true" tabindex="-1" href="#左对齐"><span class="icon icon-link"></span></a>左对齐</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> <span class="token string">'BEGIN{printf "|%-10s|\n", "hello"}'</span>
</span><span class="code-line"><span class="token comment"># |hello     |</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="通用说明符"><a aria-hidden="true" tabindex="-1" href="#通用说明符"><span class="icon icon-link"></span></a>通用说明符</h3><div class="wrap-body">

































<table><thead><tr><th align="left">特征符</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>c</code></td><td align="left">ASCII 字符</td></tr><tr><td align="left"><code>d</code></td><td align="left">十进制整数</td></tr><tr><td align="left"><code>e</code>, <code>E</code>, <code>f</code></td><td align="left">浮点格式</td></tr><tr><td align="left"><code>o</code></td><td align="left">无符号八进制值</td></tr><tr><td align="left"><code>s</code></td><td align="left">细绳</td></tr><tr><td align="left"><code>%</code></td><td align="left">文字百分比</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="space-空间"><a aria-hidden="true" tabindex="-1" href="#space-空间"><span class="icon icon-link"></span></a>Space 空间</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'{
</span></span><span class="code-line"><span class="token string">    printf "%-10s %s\n", $1, $(NF-1)
</span></span><span class="code-line"><span class="token string">}'</span> /etc/passwd <span class="token operator">|</span> <span class="token function">head</span> <span class="token parameter variable">-n</span> <span class="token number">3</span>
</span></code></pre>
<p>输出</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">root       /root
</span><span class="code-line">bin        /bin
</span><span class="code-line">daemon     /sbin
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="header-标题头"><a aria-hidden="true" tabindex="-1" href="#header-标题头"><span class="icon icon-link"></span></a>Header 标题头</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">awk</span> -F: <span class="token string">'BEGIN {
</span></span><span class="code-line"><span class="token string">    printf "%-10s %s\n", "User", "Home"
</span></span><span class="code-line"><span class="token string">    printf "%-10s %s\n", "----","----"}
</span></span><span class="code-line"><span class="token string">    { printf "%-10s %s\n", $1, $(NF-1) }
</span></span><span class="code-line"><span class="token string">'</span> /etc/passwd <span class="token operator">|</span> <span class="token function">head</span> <span class="token parameter variable">-n</span> <span class="token number">5</span>
</span></code></pre>
<p>输出</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">User       Home
</span><span class="code-line">----       ----
</span><span class="code-line">root       /root
</span><span class="code-line">bin        /bin
</span><span class="code-line">daemon     /sbin
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="各种各样的"><a aria-hidden="true" tabindex="-1" href="#各种各样的"><span class="icon icon-link"></span></a>各种各样的</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="正则表达式元字符"><a aria-hidden="true" tabindex="-1" href="#正则表达式元字符"><span class="icon icon-link"></span></a>正则表达式元字符</h3><div class="wrap-body">
<ul class="cols-3 style-none">
<li><code>\</code></li>
<li><code>^</code></li>
<li><code>$</code></li>
<li><code>.</code></li>
<li><code>[</code></li>
<li><code>]</code></li>
<li><code>|</code></li>
<li><code>(</code></li>
<li><code>)</code></li>
<li><code>*</code></li>
<li><code>+</code></li>
<li><code>?</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="转义序列"><a aria-hidden="true" tabindex="-1" href="#转义序列"><span class="icon icon-link"></span></a>转义序列</h3><div class="wrap-body">

































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>\b</code></td><td align="left">退格</td></tr><tr><td align="left"><code>\f</code></td><td align="left">换页</td></tr><tr><td align="left"><code>\n</code></td><td align="left">换行(换行)</td></tr><tr><td align="left"><code>\r</code></td><td align="left">回车</td></tr><tr><td align="left"><code>\t</code></td><td align="left">水平选项卡</td></tr><tr><td align="left"><code>\v</code></td><td align="left">垂直选项卡</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="运行脚本"><a aria-hidden="true" tabindex="-1" href="#运行脚本"><span class="icon icon-link"></span></a>运行脚本</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">cat</span> demo.awk
</span><span class="code-line"><span class="token comment">#!/usr/bin/awk -f</span>
</span><span class="code-line">BEGIN <span class="token punctuation">{</span> x <span class="token operator">=</span> <span class="token number">23</span> <span class="token punctuation">}</span>
</span><span class="code-line">      <span class="token punctuation">{</span> x <span class="token operator">+=</span> <span class="token number">2</span> <span class="token punctuation">}</span>
</span><span class="code-line">END   <span class="token punctuation">{</span> print x <span class="token punctuation">}</span>
</span><span class="code-line">$ <span class="token function">awk</span> <span class="token parameter variable">-f</span> demo.awk /etc/passwd
</span><span class="code-line"><span class="token number">69</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://www-zeuthen.desy.de/dv/documentation/unixguide/infohtml/gawk/gawk.html">GNU awk 用户指南</a> <em>(www-zeuthen.desy.de)</em></li>
<li><a href="https://gist.github.com/Rafe/3102414">AWK cheatsheet</a> <em>(gist.github.com)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js?v=1.5.2" defer></script><script src="../js/fuse.min.js?v=1.5.2" defer></script><script src="../js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
